﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using VOL;
using System.Data;

namespace Web.Admin
{
    public partial class NguoiDungForm : System.Web.UI.Page
    {
        DMCanBoBL _canBoBLL = new DMCanBoBL();
        int IDCANBO = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                FillData();

                string _IdCanBo = Request.QueryString["IDCANBO"];

                if (_IdCanBo == null || _IdCanBo == "" || !int.TryParse(_IdCanBo, out IDCANBO))
                {
                    hdfIDCANBO.Value = "0";
                }
                else
                {
                    DMCanBo obj = new DMCanBo();
                    DateTime ngaysinh = new DateTime();

                    obj.IDCanBo = IDCANBO;

                    DataTable dt = _canBoBLL.searchIDCANBO(obj);
                    txtHoTen.Text = dt.Rows[0]["HOTEN"].ToString();

                    ngaysinh = DateTime.Parse(dt.Rows[0]["NGAYSINH"].ToString().Substring(0, 10));
                    txtNgaySinh.Text = ngaysinh.ToString("yyyy-MM-dd");

                    txtPhongBan.Text = dt.Rows[0]["PHONGBAN"].ToString();

                    hdfIDCANBO.Value = _IdCanBo;

                    ddlChucVu.SelectedValue = dt.Rows[0]["IDCHUCVU"].ToString();

                    txtTenTruyCap.Text = dt.Rows[0]["TENTRUYCAP"].ToString();
                    //txtMatKhau.Text = dt.Rows[0]["MATKHAU"].ToString();\
                    txtMatKhau.Attributes.Add("value",dt.Rows[0]["MATKHAU"].ToString());

                    ckbadmin.Checked = bool.Parse(dt.Rows[0]["admin"].ToString());
                }
            }
        }

        private void FillData()
        {
            DMChucVuBL _dmChucVuBl = new DMChucVuBL();

            ddlChucVu.DataSource = _dmChucVuBl.GetData();
            ddlChucVu.DataValueField = "IDCHUCVU";
            ddlChucVu.DataTextField = "TENCHUCVU";
            ddlChucVu.DataBind();
            ddlChucVu.Items.Insert(0, "- Vui lòng Chọn -");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string _IdCanBo = hdfIDCANBO.Value;
            if (_IdCanBo == null || _IdCanBo == "0")
            {
                // thực hiện thêm mới
                DMCanBo obj = new DMCanBo();
                obj.HoTen = txtHoTen.Text;
                obj.NgaySinh = DateTime.Parse(txtNgaySinh.Text);
                obj.PhongBan = txtPhongBan.Text;
                obj.ChucVu = int.Parse(ddlChucVu.SelectedValue);
                obj.TenTruyCap = txtTenTruyCap.Text;
                obj.MatKhau = txtMatKhau.Text;
                obj.Admin = ckbadmin.Checked;

                _canBoBLL.InsertData(obj);
            }
            else
            {
                // thực hiện sửa
                DMCanBo obj = new DMCanBo();
                obj.IDCanBo = int.Parse(_IdCanBo);
                obj.HoTen = txtHoTen.Text;
                obj.NgaySinh = DateTime.Parse(txtNgaySinh.Text);
                obj.PhongBan = txtPhongBan.Text;
                obj.ChucVu = int.Parse(ddlChucVu.SelectedValue);
                obj.TenTruyCap = txtTenTruyCap.Text;
                obj.MatKhau = txtMatKhau.Text;
                obj.Admin = ckbadmin.Checked;

                _canBoBLL.UpdateData(obj);
            }

            Response.Redirect("~/Admin/NguoiDungManager.aspx");
        }
    }
}